
Page({

  /**
   * 页面的初始数据
   */
  data: {
    schema:[],
    formData:{
      latitude: 30.49984,
      longitude: 114.34253,
      scale: 18,
      name: '',
      address: '',

      type: 'wgs84',
      altitude: false,
      isHighAccuracy: false,
      highAccuracyExpireTime: 0,

      fuzzyType: 'wgs84'
    },
    title: '',
    result: '',
    remark: '',
    webUrl: '',
    apiName: '',
    showPrivacy: false
  },
  onUnload() {
    wx.offLocationChange();
    wx.offLocationChangeError();
    wx.stopLocationUpdate();
  },
  onLoad(options) {
    const that = this;
    console.log('options', options)
    wx.setNavigationBarTitle({
      title: options.title,
    })
    
    this.setData({
      title: options.title,
      zh:options.zh,
      remark: options.remark,
      webUrl: options.webUrl
    })
    if(options.zh === 'wx.openLocation') {
      this.setData({
        schema: [
          {
            label:'纬度，范围为-90~90，负数表示南纬。使用 gcj02 国测局坐标系',
            prop:'latitude',
            type:'number'
          },
          {
            label:'经度，范围为-180~180，负数表示西经。使用 gcj02 国测局坐标系',
            prop:'longitude',
            type:'number'
          },
          {
            label:'缩放比例，范围5~18',
            prop:'scale',
            type:'number'
          },
          {
            label:'位置名',
            prop:'name',
            type:'text'
          },
          {
            label:'地址的详细说明',
            prop:'address',
            type:'text'
          },
        ]
      })
    }
    if(options.zh === 'wx.startLocationUpdate' || options.zh === 'wx.stopLocationUpdate') {
      wx.getSetting({
        success(res) {
          console.log('getSetting-success', res);
          if(res.authSetting['scope.userLocation']) {
            wx.startLocationUpdate({
              type: 'gcj02',
              success(res) {
                if(options.zh === 'wx.startLocationUpdate') {
                  that.setData({
                    result: res,
                    apiName: options.zh
                  })
                }
              },
              fail(res) {
                if(options.zh === 'wx.startLocationUpdate') {
                  that.setData({
                    result: res,
                    apiName: options.zh
                  })
                }
              }
            })
          } else {
            wx.authorize({
              scope: 'scope.userLocation',
              success (res) {
                console.log('authorize-success', res);
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                wx.startLocationUpdate({
                  type: 'gcj02',
                  success(res) {
                    if(options.zh === 'wx.startLocationUpdate') {
                      that.setData({
                        result: res,
                        apiName: options.zh
                      })
                    }
                  },
                  fail(res) {
                    if(options.zh === 'wx.startLocationUpdate') {
                      that.setData({
                        result: res,
                        apiName: options.zh
                      })
                    }
                  }
                })
              },
              fail(res) {
                that.setData({
                  result: res,
                  apiName: options.zh
                })
                console.log('authorize-userLocation-fail', res);
              }
            })
          }
        },
        fail(res) {
          console.log('getSetting-fail', res);
        }
      })
    }
    if(options.zh === 'wx.startLocationUpdateBackground') {
      wx.getSetting({
        success(res) {
          console.log('getSetting-success', res);
          if(res.authSetting['scope.userLocationBackground']) {
            wx.startLocationUpdateBackground({
              type: 'gcj02',
              success(res) {
                that.setData({
                  result: res,
                  apiName: options.zh
                })
              },
              fail(res) {
                that.setData({
                  result: res,
                  apiName: options.zh
                })
              }
            })
          } else {
            wx.authorize({
              scope: 'scope.userLocationBackground',
              success (res) {
                console.log('authorize-success', res);
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                wx.startLocationUpdateBackground({
                  type: 'gcj02',
                  success(res) {
                    that.setData({
                      result: res,
                      apiName: options.zh
                    })
                  },
                  fail(res) {
                    that.setData({
                      result: res,
                      apiName: options.zh
                    })
                  }
                })
              },
              fail(res) {
                that.setData({
                  result: res,
                  apiName: options.zh
                })
                console.log('authorize-userLocation-fail', res);
              }
            })
          }
        },
        fail(res) {
          console.log('getSetting-fail', res);
        }
      })
    }
    if(options.zh === 'wx.onLocationChange' || options.zh === 'wx.offLocationChange' || options.zh === 'wx.onLocationChangeError' || options.zh === 'wx.offLocationChangeError') {
      wx.onLocationChangeError((res) => {
        console.log(res, '====res');
        this.setData({
          result: res,
          apiName: options.zh
        })
      });
      wx.onLocationChange((res) => {
        if(options.zh === 'wx.onLocationChange' || options.zh === 'wx.offLocationChange') {
          this.setData({
            result: res,
            apiName: options.zh
          })
        }
        if(options.zh === 'wx.onLocationChangeError' || options.zh === 'wx.offLocationChangeError') {
          this.setData({
            result: 'onLocationChange接口成功',
            apiName: options.zh
          })
        }
        console.log('onLocationChange==', res);
      });
      wx.getSetting({
        success(res) {
          console.log('getSetting-success', res);
          if(res.authSetting['scope.userLocation']) {
            wx.startLocationUpdate({
              type: 'gcj02',
              success(res) {
                console.log('startLocationUpdate-success', res);
              },
              fail(res) {
                console.log('startLocationUpdate-fail', res);
              }
            })
          } else if (res.authSetting['scope.userLocationBackground']) {
            wx.startLocationUpdateBackground({
              type: 'gcj02',
              success(res) {
                console.log('startLocationUpdateBackground-success', res);
              },
              fail(res) {
                console.log('startLocationUpdateBackground-fail', res);
              }
            })
          } else if (!res.authSetting['scope.userLocation']) {
            wx.authorize({
              scope: 'scope.userLocation',
              success (res) {
                console.log('authorize-success', res);
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                wx.startLocationUpdate({
                  type: 'gcj02',
                  success(res) {
                    console.log('startLocationUpdate-success', res);
                  },
                  fail(res) {
                    console.log('startLocationUpdate-fail', res);
                  }
                })
              },
              fail(res) {
                console.log('authorize-userLocation-fail', res);
              }
            })
          } else if (!res.authSetting['scope.userLocationBackground']) {
            wx.authorize({
              scope: 'scope.userLocationBackground',
              success (res) {
                console.log('authorize-success', res);
                // 用户已经同意小程序使用录音功能，后续调用 wx.startRecord 接口不会弹窗询问
                wx.startLocationUpdateBackground({
                  type: 'gcj02',
                  success(res) {
                    console.log('startLocationUpdateBackground-success', res);
                  },
                  fail(res) {
                    console.log('startLocationUpdateBackground-fail', res);
                  }
                })
              },
              fail(res) {
                console.log('authorize-userLocationBackground-fail', res);
              }
            })
          }
        },
        fail(res) {
          console.log('getSetting-fail', res);
        }
      })
    }
    if(options.zh === 'wx.getLocation') {
      this.setData({
        schema: [
          {
            label:'wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标',
            prop:'type',
            type:'picker',
            labelOptions:['wgs84', 'gcj02'], //选项
            valueOptions:['wgs84','gcj02'], //选项对应的值
          },
          {
            label:'传入 true 会返回高度信息，由于获取高度需要较高精确度，会减慢接口返回速度',
            prop:'altitude',
            type:'switch'
          },
          {
            label:'开启高精度定位',
            prop:'isHighAccuracy',
            type:'switch'
          },
          {
            label:'高精度定位超时时间(ms)，指定时间内返回最高精度，该值3000ms以上高精度定位才有效果',
            prop:'highAccuracyExpireTime',
            type:'number'
          },
        ]
      })
    }
    if(options.zh === 'wx.getFuzzyLocation') {
      this.setData({
        schema: [
          {
            label:'返回的坐标类型',
            prop:'type',
            type:'picker',
            labelOptions:['wgs84', 'gcj02'], //选项
            valueOptions:['wgs84','gcj02'], //选项对应的值
          },
        ]
      })
    }
    if(options.zh === 'wx.chooseLocation') {
      this.setData({
        schema: [
          {
            label:'目标地纬度',
            prop:'latitude',
            type:'number'
          },
          {
            label:'目标地经度',
            prop:'longitude',
            type:'number'
          },
        ]
      })
    }
  },
  getFormData(e) {
    console.log('formdata==', e.detail.formData);
    this.setData({
      formData: e.detail.formData
    })
  },
  openLocation(e) {
    wx.openLocation({
      // latitude: this.data.formData.latitude,
      // longitude: this.data.formData.longitude,
      // scale: this.data.formData.scale,
      // name: this.data.formData.name,
      // address: this.data.formData.address,
      latitude: 31.032019,
      longitude: 121.443136,
      scale: 18,
      name: '上海交通大学闵行校区',
      address: '上海市闵行区东川路800号‌‌',
      success: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开成功
      fail: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开失败
    })
  },
  offLocationChange() {
    wx.offLocationChange()
  },
  offLocationChangeError() {
    wx.offLocationChangeError()
  },
  stopLocationUpdate() {
    wx.stopLocationUpdate()
  },
  getLocation(e) {
    wx.getLocation({
      type: this.data.formData.type,
      altitude: this.data.formData.altitude,
      isHighAccuracy: this.data.formData.isHighAccuracy,
      highAccuracyExpireTime: this.data.formData.highAccuracyExpireTime,
      success: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开成功
      fail: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开失败
    })
  },
  getFuzzyLocation(e) {
    wx.getFuzzyLocation({
      type: this.data.formData.fuzzyType,
      success: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开成功
      fail: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开失败
    })
  },
  choosePoi(e) {
    wx.choosePoi({
      success: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开成功
      fail: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开失败
    })
  },
  chooseLocation(e) {
    wx.chooseLocation({
      latitude: this.data.formData.latitude,
      longitude: this.data.formData.longitude,
      success: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开成功
      fail: (res) => {
        this.setData({
          result: res,
          apiName: e.currentTarget.dataset.apiname
        })
      }, // 打开失败
    })
  }
})